package com.feifan.service.impl;

import com.feifan.service.api.MailSendService;
import freemarker.template.Template;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.core.io.FileSystemResource;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * 邮件发送实现
 * @author Donald
 * @create 2019-05-04 11:02
 */
@Service
public class MailSend implements MailSendService {

    @Value("${spring.mail.username}")
    private String sendMaster;

    @Autowired
    private JavaMailSender javaMail;

    @Override
    public boolean sendMail(String user, String title, String context) {
        //简单邮件发送
        SimpleMailMessage message = new SimpleMailMessage();
        message.setFrom(sendMaster);
        message.setTo(user);
        message.setSubject(title);
        message.setText(context);
        javaMail.send(message);
        return true;
    }

    @Override
    public boolean sendMail(String user, String title, String context, File file) {
        // 发送带附件的邮件
        MimeMessage message = javaMail.createMimeMessage();
        try {
            // 添加附件，第二个参数必须设置为true
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(sendMaster);
            helper.setTo(user);
            helper.setSubject(title);
            helper.setText(context);
            FileSystemResource resource = new FileSystemResource(file);
            helper.addAttachment("附件", resource);

        } catch (MessagingException e)
        {
            e.printStackTrace();
        }
        javaMail.send(message);
        return true;
    }

    @Autowired
    private FreeMarkerConfigurer freeMarkerConfigurer;

    @Override
    public boolean sendTemplate(String user, String title, String info) {
        // 发送模板模板邮件
        MimeMessage message = javaMail.createMimeMessage();
        try {
            MimeMessageHelper helper = new MimeMessageHelper(message, true);
            helper.setFrom(sendMaster);
            helper.setTo(user);
            helper.setSubject(title);
            // 封装模板使用的数据
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("url", "http://www.stacysi.top:8080/index");
            // 得到模板
            Template template = freeMarkerConfigurer.getConfiguration().getTemplate("mailTemplate.html");
            String html = FreeMarkerTemplateUtils.processTemplateIntoString(template, map);
            helper.setText(html, true);
        } catch (Exception e) {
            e.printStackTrace();
        }
        javaMail.send(message);
        return true;
    }
}
